package com.doucat.support.io;

import android.content.Context;
import android.content.res.AssetManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2021-2031, by tonly, All rights reserved.
 * -----------------------------------------------------------------
 * File: AssetReader.java
 * Author: wangfy
 * Create: 2021/7/14 15:35
 */
public class AssetReader {
    private Context context;

    public AssetReader(Context context) {
        this.context = context;
    }

    //读取配置文件
    public String getJsonFromFile(String filePath) {
        String result = "";
        AssetManager assetManager = context.getResources().getAssets();
        try {
            InputStream ins = assetManager.open(filePath);
            byte[] buffer = new byte[ins.available()];
            ins.read(buffer);
            ins.close();
            result = new String(buffer);
            JSONObject json = new JSONObject(result);
            result = json.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }
}